package com.ruoyi.web.controller.gd;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.gd.khxx.domain.vo.ZKhxxVo;
import com.ruoyi.system.service.ISysDictTypeService;
import lombok.RequiredArgsConstructor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gd.ldjl.domain.vo.ZLdjlVo;
import com.ruoyi.gd.ldjl.domain.bo.ZLdjlBo;
import com.ruoyi.gd.ldjl.service.IZLdjlService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 录单记录
 *
 * @author ruoyi
 * @date 2024-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gd/ldjl")
public class ZLdjlController extends BaseController {

    private final IZLdjlService iZLdjlService;

    @Resource
    private final ISysDictTypeService dictTypeService;

    /**
     * 查询录单记录列表
     */
    @GetMapping("/listAllLdjl")
    public List<ZLdjlVo> listAllLdjl(ZLdjlBo bo) {
        return iZLdjlService.queryList(bo);
    }

    /**
     * 查询录单记录列表
     */
    @SaCheckPermission("gd:ldjl:list")
    @GetMapping("/list")
    public TableDataInfo<ZLdjlVo> list(ZLdjlBo bo, PageQuery pageQuery) {
        return iZLdjlService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出录单记录列表
     */
    @SaCheckPermission("gd:ldjl:export")
    @Log(title = "录单记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZLdjlBo bo, HttpServletResponse response) {
        List<ZLdjlVo> list = iZLdjlService.queryList(bo);
        List<SysDictData> ssfgsList = dictTypeService.selectDictDataByType("gd_ssgs");
        List<SysDictData> sfList = dictTypeService.selectDictDataByType("sys_yes_no");
        for (int i = 0; i < list.size(); i++) {
            ZLdjlVo zLdjlVo = list.get(i);
            zLdjlVo.setExcelId(i + 1 + "");

            // 设置是否所属分公司
            ssfgsList.forEach(item -> {
                if (item.getDictValue().equals(zLdjlVo.getCreateDeptName())) {
                    zLdjlVo.setCreateDeptName(item.getDictLabel());
                }
            });
            // 设置下场 设置修改
            sfList.forEach(item -> {
                if (item.getDictValue().equals(zLdjlVo.getXc())) {
                    zLdjlVo.setXc(item.getDictLabel());
                }
                if (item.getDictValue().equals(zLdjlVo.getXg())) {
                    zLdjlVo.setXg(item.getDictLabel());
                }
            });
        }
        ExcelUtil.exportExcel(list, "录单记录", ZLdjlVo.class, response);
    }

    /**
     * 导出选择数据
     */
    @Log(title = "业务单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelectDat/{ids}")
    public void exportSelectData(@PathVariable("ids") String ids, HttpServletResponse response) {
        List<ZLdjlVo> list = iZLdjlService.queryListByIds(ids);
        // 获取字典数据当前所属公司
        List<SysDictData> ssfgsList = dictTypeService.selectDictDataByType("gd_ssgs");
        for (int i = 0; i < list.size(); i++) {
            ZLdjlVo zLdjlVo = list.get(i);
            zLdjlVo.setExcelId(i + 1 + "");

            // 设置是否所属分公司
            ssfgsList.forEach(item -> {
                if (item.getDictValue().equals(zLdjlVo.getCreateDeptName())) {
                    zLdjlVo.setCreateDeptName(item.getDictLabel());
                }
            });
        }
        ExcelUtil.exportExcel(list, "业务单", ZLdjlVo.class, response);
    }

    /**
     * 获取录单记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gd:ldjl:query")
    @GetMapping("/{id}")
    public R<ZLdjlVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iZLdjlService.queryById(id));
    }

    /**
     * 新增录单记录
     */
    @SaCheckPermission("gd:ldjl:add")
    @Log(title = "录单记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZLdjlBo bo) {
        return toAjax(iZLdjlService.insertByBo(bo));
    }

    /**
     * 修改录单记录
     */
    @SaCheckPermission("gd:ldjl:edit")
    @Log(title = "录单记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZLdjlBo bo) {
        return toAjax(iZLdjlService.updateByBo(bo));
    }

    /**
     * 删除录单记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gd:ldjl:remove")
    @Log(title = "录单记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iZLdjlService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
